package com.anyisn.anyflickr.shell.view
{	
	import com.adobe.webapis.flickr.AuthResult;
	import com.anyisn.anyflickr.common.ApplicationConstants;
	import com.anyisn.anyflickr.modules.PhotoInfo.view.component.PhotoInfoWindow;
	import com.anyisn.anyflickr.modules.PhotosSlide.view.component.PhotosSlideWindow;
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.UpdateProxy;
	import com.anyisn.anyflickr.shell.view.components.ThumbsList;
	import com.anyisn.anyflickr.shell.view.components.popup.About;
	import com.anyisn.anyflickr.shell.view.components.popup.PeopleWindow;
	import com.anyisn.anyflickr.shell.view.components.popup.PersonTW;
	import com.anyisn.anyflickr.shell.view.components.popup.SearchTW;
	import com.anyisn.anyflickr.shell.view.components.popup.SettingsTW;
	import com.anyisn.puremvc.as3.multicore.patterns.mediator.AbstractPopupMediator;
	
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.controls.FlexNativeMenu;
	import mx.core.IFlexDisplayObject;
	import mx.events.FlexEvent;
	import mx.events.FlexNativeMenuEvent;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class ApplicationMediator extends AbstractPopupMediator
	{
		public static const NAME:String = 'ApplicationMediator';
		private var _tl:ThumbsList;
		private var _menu:FlexNativeMenu;
		private var _thumbsTileMediator:ThumbsTileMediator;
		private var _thumbsCBMediator:ThumbsCBMediator;
		
		public function ApplicationMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		private function initNativeMenu():void {
			_menu = new FlexNativeMenu();
			var data:Array = ApplicationConstants.nativeMenuData;
			data[2].children[1].enabled = false;
			data[0].children[2].enabled = false;
			data[1].children[2].enabled = false;
			_menu.dataProvider = data;
			_menu.labelField = 'label';
			_menu.addEventListener(FlexNativeMenuEvent.ITEM_CLICK, onMenuClick);
			app.menu = _menu;
		}
		
		private function initListView():void {
			_tl = new ThumbsList();
			_tl.addEventListener(FlexEvent.CREATION_COMPLETE, onTLCreated);
			app.addChild(_tl);
		}
		
		private function onTLCreated(e:FlexEvent):void {
			_thumbsCBMediator = new ThumbsCBMediator(_tl.thumbsCB);
			_thumbsTileMediator = new ThumbsTileMediator(_tl.thumbsTile)
			facade.registerMediator(_thumbsCBMediator);
			facade.registerMediator(_thumbsTileMediator);
		}
		
		override public function onRegister() : void {
//			app.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			app.addEventListener(AnyFlickr.SHUTDOWN, onShutdown);
			secondTimer = new Timer(2000, 1);
			initNativeMenu();
			initListView();
		}

		private function onMenuClick(e:FlexNativeMenuEvent):void {
			switch(e.item.name) {
				case ApplicationConstants.STATUSBAR:
					app.showStatusBar = app.showStatusBar?false:true;
					break;
				case ApplicationConstants.FULLSCREEN:
					if(app.stage.displayState == StageDisplayState.NORMAL) {
						app.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
					} else {
						app.stage.displayState = StageDisplayState.NORMAL;
					}
					break;
				case ApplicationConstants.SETTINGS:
					openPopUpWindow(SettingsTW, SettingsTWMediator);
					break;
				case ApplicationConstants.ABOUT:
					var aboutIF:IFlexDisplayObject = PopUpManager.createPopUp(app, About, true);
					var about:About = aboutIF as About;
					var up:UpdateProxy = facade.retrieveProxy(UpdateProxy.NAME) as UpdateProxy;
					about.nameLb.label = up.appName;
					about.versionLbl.text = up.appVersion;
					about.addEventListener(MouseEvent.CLICK, removePopUp);
					PopUpManager.centerPopUp(aboutIF);
					break;
				case ApplicationConstants.UPDATE:
					sendNotification(ShellFacade.CHECK_FOR_UPDATE);
					break;
				case ShellFacade.TILE_INTERESTINGNESS_GET_LIST:
				case ShellFacade.SLIDE_INTERESTINGNESS_GET_LIST:
				case ShellFacade.TILE_FAVORITES_GET_LIST:
				case ShellFacade.SLIDE_FAVORITES_GET_LIST:
					sendNotification(e.item.name);
					break;
				case ShellFacade.TILE_PHOTOS_SEARCH:
				case ShellFacade.SLIDE_PHOTOS_SEARCH:
					var win0:IFlexDisplayObject = PopUpManager.createPopUp(app, SearchTW, true);
					var med0:SearchTWMediator = new SearchTWMediator(win0, e.item.name);
					facade.registerMediator(med0);
					PopUpManager.centerPopUp(win0);
					break;
				case ShellFacade.LOGIN:
					sendNotification(ShellFacade.LOGIN);
					break;
				case ShellFacade.LOGOUT:
					sendNotification(ShellFacade.LOGOUT);
					break;
				case ShellFacade.SHOW_PEOPLE_WIN:
					if(facade.hasMediator(PeopleWindowMediator.NAME)) {
						var med2:PeopleWindowMediator = facade.retrieveMediator(PeopleWindowMediator.NAME) as PeopleWindowMediator;
						var win2:PeopleWindow = med2.getViewComponent() as PeopleWindow;
						win2.activate();
					} else {
						var win3:PeopleWindow = new PeopleWindow();
						var med3:PeopleWindowMediator = new PeopleWindowMediator(win3);
						win3.open();
						facade.registerMediator(med3);
					}
					break;
			}
		}
		
/*		private function onKeyUp(e:KeyboardEvent):void {
			var bShiftPressed:Boolean = e.shiftKey;
			var bCrtlPressed:Boolean = e.ctrlKey;
			var key:uint = e.keyCode;
			if(bShiftPressed) {
				
			} else if(bCrtlPressed) {
				switch(key) {
					// Ctrl + D
					case 68:
						break;
					// Ctrl + F
					case 70:
						break;
				}
			} else {
				switch(key) {
					// press F11 to toogle fullscreen
					case 122:
						break;
				}
			}
		}*/
		
		private function onShutdown(e:Event):void {
			sendNotification(ShellFacade.SHUTDOWN);
		}
		
		override public function listNotificationInterests():Array {
			return [ShellFacade.PHOTO_LOAD_OPEN,
					ShellFacade.PHOTO_LOAD_PROGRESS,
					ShellFacade.PHOTO_LOAD_CANCELED,
					ShellFacade.PHOTO_LOAD_COMPLETE,
					
					ShellFacade.PHOTO_DOWNLOAD_PROGRESS,
					ShellFacade.PHOTO_DOWNLOAD_COMPLETE,
					
					ShellFacade.FAVORITES_ADD,
					ShellFacade.FAVORITES_ADD_SUCCESS,
					ShellFacade.FAVORITES_ADD_FAILED,
					ShellFacade.FAVORITES_REMOVE,
					ShellFacade.FAVORITES_REMOVE_SUCCESS,
					ShellFacade.FAVORITES_REMOVE_FAILED,
					
					ShellFacade.ONLINE,
					ShellFacade.OFFLINE,
					
					ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS,
					ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS,
					ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS,
					ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS,
					ShellFacade.SLIDE_INTERESTINGNESS_GET_LIST_SUCCESS,
					ShellFacade.SLIDE_FAVORITES_GET_LIST_SUCCESS,
					ShellFacade.SLIDE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS,
					ShellFacade.SLIDE_PHOTOS_SEARCH_SUCCESS,
					
					ShellFacade.LOGIN_SUCCESS,
					ShellFacade.LOGOUT_SUCCESS,
					
					ShellFacade.PEOPLE_GET_INFO_SUCCESS,
					ShellFacade.SHOW_PHOTO_INFO_WINDOW,
					ShellFacade.SHOW_PHOTOS_SLIDE_WINDOW
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case ShellFacade.PEOPLE_GET_INFO_SUCCESS:
					openPopUpWindow(PersonTW, PersonTWMediator);
					break;
				case ShellFacade.SHOW_PHOTO_INFO_WINDOW:
					var win0:PhotoInfoWindow = notification.getBody() as PhotoInfoWindow;
					win0.addEventListener(Event.CLOSE, onPhotoInfoWindowClose);
					win0.open();
					break;
				case ShellFacade.SHOW_PHOTOS_SLIDE_WINDOW:
					var win1:PhotosSlideWindow = notification.getBody() as PhotosSlideWindow;
					win1.addEventListener(Event.CLOSE, onPhotosSlideWindowClose);
					win1.open();
					break;
				case ShellFacade.PHOTO_DOWNLOAD_PROGRESS:
					var details:Array = notification.getBody() as Array;
					app.status = "Downloading ... " + Math.round(details[1]/1024) + "KB " + Math.round(details[0]*100/details[1]) + "%";
					break;
				case ShellFacade.PHOTO_DOWNLOAD_COMPLETE:
					app.status = "Completed!";
					delayHideStatus();
					break;
				case ShellFacade.FAVORITES_ADD:
					app.status = "Adding favorites ...";
					break;
				case ShellFacade.FAVORITES_ADD_SUCCESS:
					app.status = "Favorites Added";
					delayHideStatus();
					break;
				case ShellFacade.FAVORITES_ADD_FAILED:
					app.status = "Favorites Added Failed";
					delayHideStatus();
					break;
				case ShellFacade.FAVORITES_REMOVE:
					app.status = "Removing favorites ...";
					break;
				case ShellFacade.FAVORITES_REMOVE_SUCCESS:
					app.status = "Favorites Removed";
					delayHideStatus();
					break;
				case ShellFacade.FAVORITES_REMOVE_FAILED:
					app.status = "Favorites Removed Failed";
					delayHideStatus();
					break;
				case ShellFacade.ONLINE:
					app.status = ShellFacade.ONLINE;
					delayHideStatus();
					break;
				case ShellFacade.OFFLINE:
					app.status = ShellFacade.OFFLINE;
					delayHideStatus();
					break;
				case ShellFacade.LOGIN_SUCCESS:
					var data0:Array = ApplicationConstants.nativeMenuData;
					data0[2].children[0].enabled = false;
					data0[2].children[1].enabled = true;
					data0[0].children[2].enabled = true;
					data0[1].children[2].enabled = true;
					_menu.dataProvider = data0;
					_menu.validateNow();
					
					var result:AuthResult = notification.getBody() as AuthResult;
					app.title = 'AnyFlickr' + ' (Logged In as ' + result.user.username + ')';
					break;
				case ShellFacade.LOGOUT_SUCCESS:
					var data1:Array = ApplicationConstants.nativeMenuData;
					data1[2].children[0].enabled = true;
					data1[2].children[1].enabled = false;
					data1[0].children[2].enabled = false;
					data1[1].children[2].enabled = false;
					_menu.dataProvider = data1;
					_menu.validateNow();
					
					app.title = 'AnyFlickr (Not Logged In)';
					break;
			}
		}
		
		private function delayHideStatus():void {
			secondTimer.addEventListener(TimerEvent.TIMER_COMPLETE, hideStatus);
			secondTimer.reset();
			secondTimer.start();
		}
		
		private function hideStatus(e:TimerEvent):void {
			secondTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, hideStatus);
			app.status = "";
		}
		
		private function onPhotoInfoWindowClose(e:Event):void {
			var winToClose:PhotoInfoWindow = e.target as PhotoInfoWindow;
			sendNotification(ShellFacade.CLOSE_PHOTO_INFO_WINDOW, winToClose);
		}
		
		private function onPhotosSlideWindowClose(e:Event):void {
			var winToClose:PhotosSlideWindow = e.target as PhotosSlideWindow;
			sendNotification(ShellFacade.CLOSE_PHOTOS_SLIDE_WINDOW, winToClose);
		}
		
		private function removePopUp(e:Event):void {
			PopUpManager.removePopUp(e.target as IFlexDisplayObject);
		}

		private function get app():AnyFlickr {
			return viewComponent as AnyFlickr;
		}
		
		private var secondTimer:Timer;
	}
}